package com.org.zp.ctrl.company;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;

public class ModifyComInfoActivity extends BaseActivity implements
		OnClickListener {
	/** Called when the activity is first created. */

	private RecApp app;

	private TextView title;
	private Button back_btn;
	private Button enable_btn;

	private EditText modify_com_linkman, modify_com_address, modify_com_phone;

	private Button modify_com_bg;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.modify_company_info);

		app = (RecApp) getApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("ModifyComInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"ModifyComInfoActivity", this);
		}

		back_btn = (Button) findViewById(R.id.back);
		enable_btn = (Button) findViewById(R.id.top_right_button);
		enable_btn.setVisibility(View.INVISIBLE);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"ModifyComInfoActivity");
			}
		});
		title = (TextView) findViewById(R.id.headTitle);
		title.setText("资料修改");

		init();
	}

	private void init() {

		modify_com_bg = (Button) findViewById(R.id.modify_com_bg);
		modify_com_bg.setOnClickListener(this);

		modify_com_linkman = (EditText) findViewById(R.id.modify_com_linkman);
		modify_com_address = (EditText) findViewById(R.id.modify_com_address);
		modify_com_phone = (EditText) findViewById(R.id.modify_com_phone);

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"ModifyComInfoActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onClick(View v) {
		if (v == modify_com_bg) {
			
			new getModifyComInfo().execute(URLHelper.mPrefix);
		}
	}

	class getModifyComInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "companyModify");
			map.put("uid", app.user.getuID());
			map.put("contact", modify_com_linkman.getText().toString().trim());
			map.put("address", modify_com_address.getText().toString().trim());
			map.put("telephone", modify_com_phone.getText().toString().trim());

			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(ModifyComInfoActivity.this, "失败",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(ModifyComInfoActivity.this,
						"网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				try {
					String succeed = new JSONObject(result)
							.getString("succeed");
					if (succeed.equals("000")) {
						Toast toast = Toast.makeText(
								ModifyComInfoActivity.this, "修改成功",
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}else{
						Toast toast = Toast.makeText(
								ModifyComInfoActivity.this, "修改失败",
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		}
	}

}